<?php
/**
 * Created by PhpStorm.
 * User: Sky
 * Date: 2017/7/27
 * Time: 下午2:26
 */

namespace app\controllers;

use app\librarys\ApiResponseCode;
use app\librarys\BaseApiController;
use app\librarys\getui\GetuiClient;
use app\librarys\getui\GetuiClientMerchant;
use app\librarys\Tools;
use app\models\Ad;
use app\models\Manager;
use app\models\Merchant;
use app\models\Sms;
use app\models\User;
use app\librarys\MobilePhoneAreaQuery;
use app\librarys\RegeoSdk;

/**
 * 通用api，不需要登录
 * @package app\controllers
 */
class ApiController extends BaseApiController
{
    public function actionDemo()
    {
        $param1 = $this->_getRequestValue('param1');
        $param2 = $this->_getRequestValue('param2', 0);

        $data = [
            'return_data_0' => [$param1, $param2],
            'return_data_1' => '1',
            'return_data_2' => [
                'd_2_1' => 'a',
                'd_2_2' => 'b'
            ]
        ];

        return $this->renderJson($data);
    }

    /**
     * 验证管理员登录
     * @return string
     */
    public function actionValidateManager()
    {
        //获取post的参数
        $account = $this->_getRequestValue('m_account');
        $password = $this->_getRequestValue('password');

        //验证管理员密码，成功返回管理员信息，无效返回null
        $item = Manager::validatePassword($account, $password);
        return $this->renderJson($item, is_null($item) ? 1 : 0, is_null($item) ? '用户名或者密码错误' : 'success');
    }

    /**
     * 发送短信验证码
     * @return string
     */
    public function actionSendSmsVerificationCode()
    {
        $phone = $this->_getRequestValue('phone');

        $timeValidity = \Yii::$app->params['sms.TimeValidity'];
        $verificationCode = Sms::genSmsVerificationCode($phone, $timeValidity);

        // send sms
        Sms::sendSmsVerificationCode($phone, $verificationCode);

        return $this->renderJson();
    }

    /**
     * 商户注册
     * @return string
     */
    public function actionMerchantRegister()
    {
        $phone = $this->_getRequestValue('phone');
        $verificationCode = $this->_getRequestValue('verification_code');

        $auth = Sms::authSmsVerificationCode($phone, $verificationCode);



        if ($auth == false) {
            return $this->renderJson(null, ApiResponseCode::SmsVerificationCodeAuthFailedErrorCode, ApiResponseCode::SmsVerificationCodeAuthFailedErrorMessage);
        }

        // 注册使用手机号已经使用，不能注册。
        $item = Merchant::getByPhone($phone);
//                file_put_contents('../web/download/resgister.txt',$item);
        if ($item != null) {
            return $this->renderJson(null, ApiResponseCode::MerchantRegisterPhoneUsedErrorCode, ApiResponseCode::MerchantRegisterPhoneUsedErrorMessage);
        }

        $id = Merchant::register($phone);
        if ($id == null) {
            return $this->renderJson(null, ApiResponseCode::MerchantRegisterFailedErrorCode, ApiResponseCode::MerchantRegisterFailedErrorMessage);
        } else {
            // 登录
//            Merchant::login($phone);

            $merchant = Merchant::login($phone);
            if ($merchant == null) {
                return $this->renderJson(null, ApiResponseCode::MerchantLoginFailedErrorCode, ApiResponseCode::MerchantLoginFailedErrorMessage);
            }

            $merchant['merchant_token'] = $merchant['token'];

        return $this->renderJson(['merchant_token' => $merchant['token']]);
//            return $this->renderJson($merchant);
        }
    }

    /**
     * 商户登录
     * @return string
     */
    public function actionMerchantLogin()
    {
        $phone = $this->_getRequestValue('phone');
        $verificationCode = $this->_getRequestValue('verification_code');

        $auth = Sms::authSmsVerificationCode($phone, $verificationCode);
        if ($auth == false) {
            return $this->renderJson(null, ApiResponseCode::SmsVerificationCodeAuthFailedErrorCode, ApiResponseCode::SmsVerificationCodeAuthFailedErrorMessage);
        }

        $item = Merchant::login($phone);
        if ($item == null) {
            return $this->renderJson(null, ApiResponseCode::MerchantLoginFailedErrorCode, ApiResponseCode::MerchantLoginFailedErrorMessage);
        }

        $item['merchant_token'] = $item['token'];

//        return $this->renderJson(['merchant_token' => $item['token']]);
        return $this->renderJson($item);
    }


    public function actionMerchantLoginByYlhCode()
    {
        $code = $this->_getRequestValue('code');
        $item =  Merchant::getTokenByYlhCode($code);
        if ($item == null) {
            return $this->renderJson(null, ApiResponseCode::MerchantLoginFailedErrorCode, ApiResponseCode::MerchantLoginFailedErrorMessage);
        }
        $item['merchant_token'] = $item['token'];
        return $this->renderJson($item);
    }

    /**
     * 用户注册
     * @return string
     */
    public function actionUserRegister()
    {
        $phone = $this->_getRequestValue('phone');
        $verificationCode = $this->_getRequestValue('verification_code');

        $auth = Sms::authSmsVerificationCode($phone, $verificationCode);
        if ($auth == false) {
            return $this->renderJson(null, ApiResponseCode::SmsVerificationCodeAuthFailedErrorCode, ApiResponseCode::SmsVerificationCodeAuthFailedErrorMessage);
        }

        // 注册使用手机号已经使用，不能注册。
        $item = User::getByPhone($phone);
        if ($item != null) {
            return $this->renderJson(null, ApiResponseCode::UserRegisterPhoneUsedErrorCode, ApiResponseCode::UserRegisterPhoneUsedErrorMessage);
        }

        $id = User::register($phone);
        if ($id == null) {
            return $this->renderJson(null, ApiResponseCode::UserRegisterFailedErrorCode, ApiResponseCode::UserRegisterFailedErrorMessage);
        } else {
            // 登录
            $user = User::login($phone);
            if ($user == null) {
                return $this->renderJson(null, ApiResponseCode::UserLoginFailedErrorCode, ApiResponseCode::UserLoginFailedErrorMessage);
            }
            // 发送注册奖励6元
            User::presentRegistGift($id,6);
            return $this->renderJson(['user_token' => $user['token']]);
        }
    }

    /**
     * 用户登录
     * @return string
     */
    public function actionUserLogin()
    {
        $phone = $this->_getRequestValue('phone');
        $verificationCode = $this->_getRequestValue('verification_code');

        $auth = Sms::authSmsVerificationCode($phone, $verificationCode);
        if ($auth == false) {
            return $this->renderJson(null, ApiResponseCode::SmsVerificationCodeAuthFailedErrorCode, ApiResponseCode::SmsVerificationCodeAuthFailedErrorMessage);
        }

        $item = User::login($phone);
        if ($item == null) {
            return $this->renderJson(null, ApiResponseCode::UserLoginFailedErrorCode, ApiResponseCode::UserLoginFailedErrorMessage);
        }

        return $this->renderJson(['user_token' => $item['token']]);
    }

    /**
     * 返回app版本号
     */
    public function actionGetAppVersion()
    {
        $app_code = $this->_getRequestValue('app_code');
        $app_type = $this->_getRequestValue('app_type');

        $item = Manager::getAppVersion(strtolower($app_code), $app_type);
        return $this->renderJson($item, $item < 1 ? 1 : 0, $item < 1 ? 'no data' : 'success');

    }

    /**
     * 更新定点广告用户
     * 需要定时任务调用
     */
    public function actionUpdateAreaAdUsers()
    {
        Merchant::updateAreaAdUsers();
    }

    /**
     * 更新用户年龄
     * 需定时任务调用
     */
    public function actionUpdateUserAge()
    {
        User::updateUserAge();
    }

    /**
     * 检查到期广告退款
     * 定时任务调用
     */
    public function actionUpdateEndAd()
    {
        $result = Ad::returnAdFee();//返回已到期未返款广告
        return $result;
    }

    public function actionTest()
    {
//        GetuiClient::pushMessageToSingle('21f206bfe062c238c3ec06628bac6646','收到了嘛？李志');
//        GetuiClient::pushMessageToSingle('21f206bfe062c238c3ec06628bac6646','收到了嘛？李志2');
//        User::presentRegistGift('525aca26-76be-11e7-84d9-00163e06b8e1',6);
//        self::actionUpdateEndAd();
//        GetuiClientMerchant::pushMessageToSingle('a0cbdbd5445fc599fcba27f59f235ee3','商家端发送');

//       Merchant::test();
        $id = Tools::db_uuid();
        Merchant::sendYLHPoint($id,100,'屏主广告','屏主广告','df3352bd-c634-11e7-84d9-00163e06b8e1');
    }

}